package com.csx.springcloud.configcenter.nacos.sample.service;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.csx.springcloud.configcenter.nacos.sample.listener.NacosConfigListener;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2024/12/24 周二 16:33
 */
@Service
public class NacosConfigCenterSampleService {

    private final Logger logger = LoggerFactory.getLogger(NacosConfigCenterSampleService.class);

    @Value("${nacos.test.atValue:defaultValue}")
    private String atValue;

    @Autowired
    private NacosConfigManager nacosConfigManager;

    private final String dataId = "test1.properties";
    private final String group = "DEFAULT_GROUP";
    private ConfigService configService;

    @PostConstruct
    private void init() {
        configService = nacosConfigManager.getConfigService();
        String config;
        try {
            config = configService.getConfig(dataId, group, 3000);
            // 初始化配置
            if(!StringUtils.hasText(config)) {
                config = """
                        nacos.test.atValue=xxx\r
                        nacos.test.1=100\r
                        nacos.test.2=kkk\r
                        """;
                boolean flag = configService.publishConfig(dataId, group, config);
                logger.info("config not init. init config success");
            }

            // 给 (dataId,group) 的配置添加配置监听器,当配置发生变更时,会触发该监听器的方法
            configService.addListener(dataId, group, new NacosConfigListener());

        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    public String testAtValueFromNacos() {
        return "@Value从nacos配置中心获取到的数据: " + atValue;
    }

    /**
     * 获取配置
     * @return
     */
    public String testGetConfigFromNacos() {
        String config = "";
        try {
            config = configService.getConfig(dataId, group, 3000);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
        return config;
    }

    /**
     * 推送配置
     * @return
     */
    public String testPublishConfigToNacos() {
        String config;
        boolean flag;
        try {
            config = configService.getConfig(dataId, group, 3000);
            config = config + "nacos.test.add=newAdd\r\n";
            flag = configService.publishConfig(dataId, group, config);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
        return flag ? "推送配置成功" : "推送配置失败";
    }

    /**
     * 删除配置
     * @return
     */
    public String testRemoveConfigFromNacos() {
        boolean flag;
        try {
            flag = configService.removeConfig(dataId, group);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
        return flag ? "删除配置成功" : "删除配置失败";
    }

}
